﻿using MyNeighbourhood.Data;
using MyNeighbourhood.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace MyNeighbourhood.Managers
{
    public class ParticipantsManager : BaseManager
    {
        public Participant CreateParticipant(int eventId, ParticipantModel model)
        {
            try
            {
                var repeated = dbContext.Participants.FirstOrDefault(x => x.Name == model.Name && x.Programme == model.Programme);
                if (repeated != null)
                {
                    return repeated;
                }

                var participant = new Participant();
                participant.Name = model.Name;
                if (eventId == 0)
                {
                    participant.Photo = "\\Images\\custom\\default-avatar.jpg";
                }
                participant.Programme = model.Programme;

                var result = dbContext.Participants.Add(participant);

                dbContext.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}